<script lang="ts" setup>
import { reactive, ref, defineAsyncComponent, computed, onMounted } from 'vue'
import dayjs from 'dayjs'
import { QueryMessage } from '@/api/operation'
import { QueryMessageTableCol } from '@/api/operation/types/operation'
import { useTable } from '@/hooks/useTable'
import { getToken } from '@/utils/cache/cookies'
import { FormInstance } from 'element-plus'

const SendMessageDialog = defineAsyncComponent(() => import('@/components/dialog/SendMessage.vue'))
const form = reactive({
  phone: '',
  type: ''
})
const formRef = ref<FormInstance | null>(null)
const SendMessageDialogRef = ref<InstanceType<typeof SendMessageDialog> | null>(null)
const openDialog = () => {
  SendMessageDialogRef.value?.open()
}
const changeState = computed(() => {
  return (val: '0' | '1') => {
    if (val == '0') {
      return '未读'
    } else if (val == '1') {
      return '已读'
    }
  }
})
const { data, loading, pagination, load, handleCurrentChange, handleSizeChange, setCurrentPage } =
  useTable<QueryMessageTableCol>(QueryMessage, getToken() ?? '', () => form)
const handleFormSearch = () => {
  setCurrentPage(1)

  load()
}
const reset = () => {
  formRef.value?.resetFields()
}
onMounted(() => {
  load()
})
</script>

<template>
  <div class="xy-container">
    <el-form :model="form" ref="formRef" inline>
      <el-form-item prop="phone" label="用户手机号">
        <el-input v-model="form.phone" placeholder="请输入用户手机号" />
      </el-form-item>
      <el-form-item prop="type" label="消息类型">
        <el-select v-model="form.type" style="width: 120px">
          <el-option label="通知" value="通知" />
          <el-option label="活动" value="活动" />
          <el-option label="运营" value="运营" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleFormSearch">查询</el-button>
        <el-button plain @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item float-right>
        <el-button type="primary" @click="openDialog">发送消息</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data" v-loading="loading" class="no-border">
      <el-table-column prop="userId" label="用户手机号" align="center" />
      <el-table-column prop="messageType" label="消息类型" align="center" />
      <el-table-column
        prop="content"
        label="消息详情"
        :show-overflow-tooltip="true"
        align="center"
      />
      <el-table-column prop="messageData" label="发送人" align="center" />
      <el-table-column prop="createdTime" label="发送时间" align="center">
        <template #default="scope">
          <span>{{ dayjs(scope.row.createdTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="阅读状态" align="center">
        <template #default="scope">
          <span
            :style="{
              color: scope.row.status == '0' ? 'var(--xy-danger-color)' : 'var(--xy-success-color)'
            }"
            >{{ changeState(scope.row.status) }}</span
          >
        </template>
      </el-table-column>
    </el-table>
    <div flex justify-end mt-20>
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :total="pagination.total"
        layout="total,prev,pager,next,jumper,sizes"
      />
    </div>
    <send-message-dialog ref="SendMessageDialogRef" />
  </div>
</template>

<style scoped></style>
